package com.weishu.core.service.impl;

import com.weishu.common.core.web.domain.AjaxResult;
import com.weishu.common.security.utils.SecurityUtils;
import com.weishu.core.entity.SysUser;
import com.weishu.core.entity.UserEntity;
import com.weishu.core.mapper.SysUserMapper;
import com.weishu.core.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author zgx
 * @since 2021-10-19
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, UserEntity> implements ISysUserService {

    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public AjaxResult registerUser(UserEntity sysUser) {
        String code = (String) redisTemplate.opsForValue().get(sysUser.getMobile());
        if ("".equals(code) || code==null){
            return AjaxResult.error("验证码已过期，请重新发送！");
        }
        //校验短信验证码
        System.out.println(code);
        if (code.equals(sysUser.getCode())){
            //正确
            String encryptPassword = SecurityUtils.encryptPassword(sysUser.getPassword());
            sysUser.setPassword(encryptPassword);
            Integer uid = this.getBaseMapper().register(sysUser);
            return AjaxResult.success("注册成功！",sysUser.getUser_id());
        }
        return AjaxResult.error("验证码错误！");
    }

    @Override
    public UserEntity getByPhone(String phone) {
        return this.getBaseMapper().getByPhone(phone);
    }
}
